/*
 * @Author: your name
 * @Date: 2021-08-20 09:48:33
 * @LastEditTime: 2021-08-29 19:51:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \vue电商管理平台\vue-e-commerce\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'

// import Users from '../components/user/Users'
// import Rights from '../components/rights/Rights'
// import Roles from '../components/rights/Roles'

// import Cate from '../components/goods/Cate'
// import Params from '../components/goods/Params'

// import goodsList from '../components/goods/List'
// import AddGoods from '../components/goods/Add'

// import Order from '../components/order/Order'

// import Report from '../components/report/Report'


// Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: "/login".
// const Login = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Home')
// const Home = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Login')
// const Welcome = () => import(/* webpackChunkName: "login_home_welcome" */ '../components/Welcome')

const Users = () => import(/* webpackChunkName: "user" */ '../components/user/Users')
const Rights = () => import(/* webpackChunkName: "user" */ '../components/rights/Rights')
const Roles = () => import(/* webpackChunkName: "user" */ '../components/rights/Roles')

const Cate = () => import(/* webpackChunkName: "cate" */ '../components/goods/Cate')
const Params = () => import(/* webpackChunkName: "cate" */ '../components/goods/Params')

const goodsList = () => import(/* webpackChunkName: "goods" */ '../components/goods/List')
const AddGoods = () => import(/* webpackChunkName: "goods" */ '../components/goods/Add')

const Order = () => import(/* webpackChunkName: "order" */ '../components/order/Order')

const Report = () => import(/* webpackChunkName: "report" */ '../components/report/Report')



Vue.use(VueRouter)


const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect:'/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: goodsList
      },
      {
        path: '/goods/add',
        component: AddGoods
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  
  const token = window.sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
